#include "gamewindow2.h"

GameWindow2::GameWindow2(QWidget *parent) : QLabel(parent) {
    // 设置窗口标题和大小
    setWindowTitle("Game Window2");
    resize(800, 600);  // 设定窗口大小

    puppy = new QLabel(this);
    puppy->setScaledContents(true);
    puppy->resize(200, 200);  // 设定小狗的大小

    // 让小狗出现在窗口底部中央
    int xPos = width() / 2 - puppy->width() / 2;
    int yPos = height() - 200;
    puppy->move(xPos, yPos);

    // 让小狗播放动画（GIF）
    QMovie *puppyMovie = new QMovie("D:/QtCreator/HeartIslandTest/figures/Angry Anger Management Sticker.gif");
    puppy->setMovie(puppyMovie);
    puppyMovie->start();  // 开始播放 GIF
    puppy->setScaledContents(true);  // 让 GIF 适应 QLabel


    puppy->show();

    // 定时器降低愤怒值
    angerTimer = new QTimer(this);
    angerbar   = new QProgressBar(this);
    angerbar->setGeometry(10,10,150,20);
    angerbar->setRange(0,100);
    angerbar->setValue(angerValue);
    angerbar->setStyleSheet("QProgressBar::chunk{background-color:pink};");

    connect(angerTimer, &QTimer::timeout, [this]() {
        if (angerValue > 0) {
            angerValue -= 2;
            updateExpression();
        }
    });
    angerTimer->start(1000);
    angerbar->setValue(angerValue);
}

void GameWindow2::mousePressEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
        angerValue = qMax(0, angerValue - 10); // 每次点击降低愤怒值
        updateExpression();
        angerbar->setValue(angerValue);
    }
}

void GameWindow2::updateExpression() {
    if (currentMovie) {  // 删除之前的动画，防止内存泄漏
        delete currentMovie;
        currentMovie = nullptr;
    }

    if (angerValue > 50)
        currentMovie = new QMovie("D:/QtCreator/HeartIslandTest/figures/Cry Crying Sticker.gif");
    else if (angerValue > 10)
        currentMovie = new QMovie("D:/QtCreator/HeartIslandTest/figures/Rabbit No Sticker.gif");
    else
        currentMovie = new QMovie("D:/QtCreator/HeartIslandTest/figures/Happy Couple Sticker.gif");

    if (currentMovie) {
        puppy->setMovie(currentMovie);
        currentMovie->start();  // 开始播放动画
    }
}
void GameWindow2::closeEvent(QCloseEvent *event) {
    emit gameWindow2Closed();
    QWidget::closeEvent(event);
}

